package com.example.test4;




public class YesterDate {

    public static int[] yesterDate(int year, int month, int day) {

        // 检查日期是否超出月份的最大天数
        int maxDays = getDaysInMonth(year, month);
        if (day > maxDays) {
            throw new IllegalArgumentException("Invalid date: day " + day + " is not valid for month " + month);
        }

        if (day > 1) {
            return new int[]{year, month, day - 1};
        } else {
            if (month == 1) {
                return new int[]{year - 1, 12, 31};
            } else {
                int prevMonth = month - 1;
                int prevDay = getDaysInMonth(year, prevMonth);
                return new int[]{year, prevMonth, prevDay};
            }

        }
    }

    private static int getDaysInMonth(int year, int month) {
        switch (month) {
            case 4:
            case 6:
            case 9:
            case 11:
                return 30;
            case 2:
                return isLeapYear(year) ? 29 : 28;
            default:
                return 31;
        }
    }

    private static boolean isLeapYear(int year) {
        return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
    }
}
